package com.tiancheng.trade.commom.core.utils;

import com.tiancheng.trade.commom.core.data.MessageBody;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Optional;
import java.util.TreeMap;
import java.util.UUID;

/**
 * 消息工具类
 */
public class MessageUtils {

    /**
     * 组织生成消息内容
     *
     * @param data
     * @return
     */
    public static MessageBody getMsgData(String messageType, Object data) {
        MessageBody messageBody = new MessageBody();
        messageBody.setMessageId(IdWorkerUtils.getInstance().buildPartNumber());
        messageBody.setMessageType(messageType);
        messageBody.setData(data);
        messageBody.setNonce(UUID.randomUUID().toString().replace("-", ""));
        messageBody.setTimestamp(LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
        return messageBody;
    }

    public static MessageBody sign(MessageBody messageBody,String signKey){
        Optional<String> sign = MessageSign.get(JsonUtil.fromJson(JsonUtil.toJsonString(messageBody), TreeMap.class), signKey);
        messageBody.setSign(sign.get());
        return messageBody;
    }

}
